package com.yunzheng.studyLeaveSystem.modules.user.adjust.record.servlet; /**
 * Created with IntelliJ IDEA.
 *
 * @Author: YunZheng
 * @Date: 2023/01/29/13:28
 * @Description:
 */

import com.alibaba.fastjson.JSON;
import com.yunzheng.studyLeaveSystem.modules.response.ResponseWrapper;
import com.yunzheng.studyLeaveSystem.modules.user.adjust.adjust.service.AdjustService;
import com.yunzheng.studyLeaveSystem.modules.user.adjust.record.entity.AdjustRecord;
import com.yunzheng.studyLeaveSystem.modules.user.adjust.record.service.RecordService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/*
 *将该用户本周调研学的数据传递给前端
 */
@WebServlet(name = "AdjustRecordServlet", value = "/AdjustRecordServlet")
public class AdjustRecordServlet extends HttpServlet {
    private RecordService recordService = new RecordService();
    private AdjustService adjustService = new AdjustService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.setAttribute("id",7);
        int id = (int) session.getAttribute("id");
        List<AdjustRecord> records = null;
        try {
            records = recordService.record(id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        ResponseWrapper responseWrapper = null;
        if (records == null || records.size() == 0) {
             responseWrapper = ResponseWrapper.SuccessButNoData();
        }
        else {
            responseWrapper = ResponseWrapper.Success(records);
        }
        String s = JSON.toJSONString(responseWrapper);
        response.getWriter().println(s);
        System.out.println(s);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
